<template>
  <div>
    <!-- 新增修改表单 -->
    <el-dialog
      width="50%"
      :title="textMap[dialogStatus]"
      :visible.sync="DialogFormVisibleCom"
      append-to-body
    >
      <div class="myfrom1002">


        <el-form ref="dataForm" :rules="rules" :model="formSubmitDataCom" label-position="right" label-width="110px"
                 style="width: 90%; margin:10px auto;">

          <el-row :gutter="20">
           
            <el-col :span="12">
              <!-- 表名 -->
              <el-form-item label="表名" prop="table_name">
                <el-select 
                  v-model="formSubmitDataCom.table_name" 
                  class="filter-item" 
                  placeholder=""
                  @change="tableNameChange"
                  >
                  <el-option v-for="item in tableTypeOptions" :key="item.table_name" :label="item.table_comment" :value="item.table_name"/>
                </el-select>
              </el-form-item>
            </el-col>
              <el-col :span="12">
              <!-- 字段名 -->
              <el-form-item label="字段名" prop="field_name">
                <el-select 
                  v-model="formSubmitDataCom.field_name" 
                  class="filter-item" 
                  placeholder=""
                  >
                  <el-option v-for="item in fieldNameOptions" :key="item.column_name" :label="item.column_comment" :value="item.column_name"/>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <!-- 主子类型 -->
              <el-form-item label="主子类型" prop="ps_type">
                <el-select v-model="formSubmitDataCom.ps_type" class="filter-item" placeholder=""
                          >
                  <el-option v-for="item in psTypeOptions" :key="item.code" :label="item.name" :value="item.code"/>
                </el-select>
              </el-form-item>
            </el-col>
              
           

            <el-col :span="12">
              <!-- 字段含义 -->
              <el-form-item label="字段含义" prop="field_meaning">
                <el-input
                  v-model="formSubmitDataCom.field_meaning"
                />
              </el-form-item>
            </el-col>

            <template v-if="isParent">
            <el-col :span="12">
              <el-form-item label="父表名" prop="p_table_name">
                 <el-select 
                  v-model="formSubmitDataCom.p_table_name" 
                  class="filter-item" 
                  placeholder=""
                  @change="pTableNameChange"
                  >
                  <el-option v-for="item in tableTypeOptionsfu" :key="item.table_name" :label="item.table_comment" :value="item.table_name"/>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="父字段名" prop="p_field_name">
                <el-select 
                  v-model="formSubmitDataCom.p_field_name" 
                  class="filter-item" 
                  placeholder=""
                  >
                  <el-option v-for="item in pFieldNameOptions" :key="item.column_name" :label="item.column_comment" :value="item.column_name"/>
                </el-select>
              </el-form-item>
            </el-col>
            </template>
            

            <el-col :span="24">
              <!-- 备注 -->
              <el-form-item label="备注：" prop="remark">
                <el-input type="textarea" v-model="formSubmitDataCom.remark" placeholder="Please select"></el-input>
              </el-form-item>
            </el-col>
          </el-row>


        </el-form>
      </div>
      <div slot="footer" class="dialog-footer" style="text-align:center">
        <el-button @click="resetFormData">
          重置
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          提交
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import XEUtils from 'xe-utils'

import SubmitFormAItem from "./SubmitFormAItem"

export default {
  name: 'submitForm',
  components: {
    SubmitFormAItem
  },
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false
    },
    dialogStatus: {
      type: String,
    },
    // tableColumn: {
    //     type: Array,
    //     default() {
    //         return []
    //     }
    // },
    formSubmitData: {
      type: Object,
      default: function () {
        return {
          id: undefined,
        table_name: '', 
        field_name: '', 
        field_meaning: '', 
        p_table_name: '',   
        p_field_name: '',   
        ps_type: '',   
        }
      }
    },
    
    tableTypeOptions:{
      type: Array
    },
    tableTypeOptionsfu:{
      type: Array
    },
    psTypeOptions:{
      type: Array
    },
    fieldNameOptions:{
      type: Array
    },
    pFieldNameOptions:{
      type: Array
    },
  },
  // 定义属性
  data() {

    return {
      textMap: {
        update: '修改',
        create: '新增'
      },
      rules: {
        table_name: [{required: true, message: '表名必填', trigger: 'change'}],
        field_name: [{required: true, message: '字段名必填', trigger: 'change'}],
        field_meaning: [{required: true, message: '映射后字段含义必填', trigger: 'change'}],
        ps_type: [{required: true, message: '主子类型必填', trigger: 'change'}],
        
      },
      isParent:false
      // 表单用到的值
      // calendarTypeOptions,
      // statusOptions: ['published', 'draft', 'deleted'],
    }
  },
  // 计算属性，会监听依赖属性值随之变化
  computed: {
    DialogFormVisibleCom: {
      get() {
        return this.dialogFormVisible
      },
      set(val) {
        this.$emit('update:dialogFormVisible', val)
      }
    },
    formSubmitDataCom: {
      get() {
        return this.formSubmitData
      },
      set(val) {
        this.$emit('update:formSubmitData', val)
      }
    },
    formSubmitDataCom_ps_type(){
      return this.formSubmitData.ps_type
    }
  },
  // 监控data中的数据变化
  watch: {
    formSubmitDataCom_ps_type(nVal,oVal){
      console.log('nval',nVal);
      switch (nVal) {
        case "1":
          this.isParent = false
          break;
        case "2":
          this.isParent = true
          break;
      
        default:
          this.isParent = false
          break;
      }
    }
  },
  // 方法集合
  methods: {
    tableNameChange(cVal){
      this.$emit('tableNameChangeEmit',cVal,'table_name')
      console.log(cVal);
    },
    pTableNameChange(cVal){
      this.$emit('tableNameChangeEmit',cVal,'p_table_name')
      console.log(cVal);
    },
    resetFormData() {
      console.log("IIIIIIIIIIUUUUUUUUUUUUUUUUU");
      let exclude = []
      if (this.dialogStatus === 'create') {
        exclude = [
        ]
      }
      console.log('this.dialogStatusthis.dialogStatus', this.dialogStatus);
      if (this.dialogStatus === 'update') {
        exclude = [
        ]
      }
      console.log('exclude', exclude);

      this.$emit('resetFormDataEmit', {
        exclude
      })

    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$emit('handleFromCreateDataEmit')
          // this.temp.id = parseInt(Math.random() * 100) + 1024 // mock a id
          // this.temp.author = 'vue-element-admin'
          // createArticle(this.temp).then(() => {
          //   this.list.unshift(this.temp)
          //   this.dialogFormVisible = false
          //   this.$notify({
          //     title: 'Success',
          //     message: 'Created Successfully',
          //     type: 'success',
          //     duration: 2000
          //   })
          // })
        }
      })
    },

    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$delete(this.formSubmitData, "_XID");
          const tempData = Object.assign({}, this.formSubmitData)
          this.$emit('handleFromUpdateDataEmit')
          // tempData.timestamp = +new Date(tempData.timestamp) // change Thu Nov 30 2017 16:41:05 GMT+0800 (CST) to 1512031311464
          // updateArticle(tempData).then(() => {
          //   const index = this.list.findIndex(v => v.id === this.temp.id)
          //   this.list.splice(index, 1, this.temp)
          //   this.dialogFormVisible = false
          //   this.$notify({
          //     title: 'Success',
          //     message: 'Update Successfully',
          //     type: 'success',
          //     duration: 2000
          //   })
          // })
        }
      })
    },

  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {

  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {

  },
  beforeCreate() {
  }, // 生命周期 - 创建之前
  beforeMount() {
  }, // 生命周期 - 挂载之前
  beforeUpdate() {
  }, // 生命周期 - 更新之前
  updated() {
  }, // 生命周期 - 更新之后
  beforeDestroy() {
  }, // 生命周期 - 销毁之前
  destroyed() {
  }, // 生命周期 - 销毁完成
  activated() {
  }, // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>

<style lang='scss' scoped>
.myfrom1002 {
  ::v-deep {
    .el-col-12 {
      height: 58px;
    }

  }
}


</style>